$(document).ready(function(){
	var token=localStorage.getItem("token");
	var id = tl_config.his_url;
	id = id.substring(id.indexOf("id=")+3, id.length);
	id = id.substring(0, id.indexOf("&"));
	$.ajax({
		url:server_url+"rest/assignOperation.do?timeStamp="+new Date().getTime(),
		type:"get",
		data:{
			postId:id,
			token:token
		},
		async:false,
		success:function(data){
			if(data.retCode='0000'){
				var settings={
					keyName:"menuId",
					parentKeyName:"parentId",
					childKeyName:"children"
				};
				var nodes=data.data.sysMenu;
				var checkedOp=data.data.checkedOp;
				var t=$.parseJSON(transformTozTreeFormat(settings,nodes));
				var menuArr=new Array();
				//遍历树结构直至叶子节点，获取从一级菜单到叶子节点的菜单名，以逗号分隔的形式保存在数组中
				//若有三级菜单，则再保存三级菜单id
				//例["系统名称","一级菜单名","二级菜单名","三级菜单名","postId"]
				for(var i in t){
					var tmp1=t[i];
					if(tmp1.children!=''&&tmp1.children!=undefined){
						for(var j in tmp1.children){
							var tmp2=tmp1.children[j];
							if(tmp2.children!=''&&tmp2.children!=undefined){
								for(var k in tmp2.children){
									var tmp3=tmp2.children[k];
									menuArr.push(tmp1.sysName+","+tmp1.menuName+","+tmp2.menuName+","+tmp3.menuName+","+tmp3.menuId);
								}
							}else{
								menuArr.push(tmp1.sysName+","+tmp1.menuName+","+tmp2.menuName);
							}
						}
					}else{
						menuArr.push(tmp1.sysName+","+tmp1.menuName);
					}
				}
				var html_str='';
				html_str+='<table>';
				html_str+='<tr>';
				html_str+='<th width="180">系统名称</th>';
				html_str+='<th width="160">一级菜单</th>';
				html_str+='<th width="160">二级菜单</th>';
				html_str+='<th width="160">三级菜单</th>';
				html_str+='<th width="290">功能名称</th>';
				html_str+='</tr>';
				for(var i in menuArr){
					var tmp=menuArr[i].split(",");
					html_str+='<tr>';
					html_str+='<td>'+(tmp[0]!=undefined?tmp[0]:"")+'</td>';
					html_str+='<td>'+(tmp[1]!=undefined?tmp[1]:"")+'</td>';
					html_str+='<td>'+(tmp[2]!=undefined?tmp[2]:"")+'</td>';
					html_str+='<td>'+(tmp[3]!=undefined?tmp[3]:"")+'</td>';
					html_str+='<td>';
					//如果三级菜单存在，则会有分配操作功能的选项
					if(tmp[3]!=null&&tmp[3]!=undefined){
						$.ajax({
							type:"get",
							url:server_url+"rest/getOpByMenuId.do?ran="+Math.random(),
							async:false,
							data:{
								menuId:tmp[4],
								token:token
							},
							success:function(data){
								if(data.retCode='0000'){
									//设置隐藏域，存放三级菜单的id，与对应功能操作绑定
									html_str+='<input type="hidden" value='+tmp[4]+' />';

									//当前菜单下选中的功能操作
									var cur_op=new Array();
									for(var i in checkedOp){
										if(checkedOp[i].menuId==tmp[4]){
											cur_op.push(checkedOp[i].opId);
										}
									}
									var op=data.data;
									if(op.length>0){
										html_str+='<div class="l_checkdiv"><input type="checkbox" value="全选"/><span>全选</span></div>';
										for(var i in op){
											if(cur_op.indexOf(op[i].opId)!=-1){
												html_str+='<div class="l_checkdiv"><input type="checkbox" checked="checked" value='+op[i].opId+'><span>'+op[i].opName+'</span></div>';
											}else{
												html_str+='<div class="l_checkdiv"><input type="checkbox" value='+op[i].opId+'><span>'+op[i].opName+'</span></div>';
											}
										}
									}
								}
							}
						});
					}
					html_str+='</td>';
					html_str+='</tr>';
				}
				html_str+='</table>';
				$("div.z_gangwei").html(html_str);

			}else {
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}

		},
	});

	$("input[value='全选']").live('click',function(){
		if(this.checked==true){
			$(this).parent().siblings().find("input").attr("checked",true);
		}else{
			$(this).parent().siblings().find("input").attr("checked",false);
		}
	});

	$(".z_sure").click(function(){
		var opArr=new Array();
		//将三级菜单id和对应的功能操作存放在数组中
		$(".z_gangwei tr td:has(input)").each(function(i,v){
			//存放三级菜单id，op_id1,op_id2..
			var opArr1=new Array();
			$(v).find("input").each(function(ii,vv){
				if($(vv).is(':hidden')){
					opArr1.push($(vv).val());
				}
				if($(vv).is(':checked')){
					if($(vv).val()!="全选"){
						opArr1.push($(vv).val());
					}
				}
			});
			//含有功能操作的一行记录作为opArr的一个元素
			opArr.push(opArr1);
		});
		$.ajax({
			type:"post",
			url:server_url+"rest/saveOperation.do?token="+token+"&timeStamp="+new Date().getTime(),
			data:{
				opArr:opArr,
				postId:id,
				token:token
			},
			traditional:true,
			success:function(data){
				if(data.retCode=='0000'){
					var url='postList.do?ran='+Math.random();
					tl_config.loadToWorkArea(url);
				}else {
					easyDialog.open({
						container:{
							header:'温馨提示',
							content:data.retMsg
						},
						autoClose : 2000
					});
				}
			}
		});
	});

	$(".z_goback").click(function(){
		var url='assignMenu.do?id='+id+'&ran='+Math.random();
		tl_config.loadToWorkArea(url);
	});
});